using System;
using UnityEngine;

namespace NodeCanvas
{
	public abstract class ActionTask : Task
	{
		[SerializeField]
		[HideInInspector]
		private float _deltaDelay;

		private Action<ValueType> FinishCallback;

		public float deltaDelay
		{
			get
			{
				return _deltaDelay;
			}
			set
			{
				_deltaDelay = value;
			}
		}

		public float elapsedTime { get; private set; }

		public bool isRunning { get; private set; }

		public bool isPaused { get; private set; }

		public virtual float estimatedLength { get; private set; }

		public sealed override string summaryInfo
		{
			get
			{
				return ((!base.agentIsOverride) ? string.Empty : "* ") + info;
			}
		}

		protected virtual string info
		{
			get
			{
				return base.taskName;
			}
		}

		public void ExecuteAction(Component agent, Action<ValueType> callback)
		{
			ExecuteAction(agent, base.blackboard, callback);
		}

		public void ExecuteAction(Component agent, Blackboard blackboard, Action<ValueType> callback)
		{
			if (!isRunning && Set(agent, blackboard))
			{
				FinishCallback = callback;
				isRunning = true;
				isPaused = false;
				base.enabled = true;
				OnExecute();
				if (isRunning)
				{
					MonoManager.current.AddMethod(UpdateAction);
				}
			}
		}

		private void UpdateAction()
		{
			elapsedTime += Time.deltaTime;
			OnUpdate();
		}

		protected virtual void OnExecute()
		{
		}

		protected virtual void OnUpdate()
		{
		}

		public void EndAction()
		{
			EndAction(true);
		}

		public void EndAction(ValueType param)
		{
			if (isRunning || isPaused)
			{
				if (elapsedTime > 0f)
				{
					MonoManager.current.RemoveMethod(UpdateAction);
					estimatedLength = elapsedTime;
					elapsedTime = 0f;
				}
				base.enabled = false;
				isRunning = false;
				isPaused = false;
				OnStop();
				if (FinishCallback != null)
				{
					FinishCallback(param);
				}
				FinishCallback = null;
			}
		}

		protected virtual void OnStop()
		{
		}

		public void PauseAction()
		{
			if (isRunning)
			{
				MonoManager.current.RemoveMethod(UpdateAction);
				base.enabled = false;
				isRunning = false;
				isPaused = true;
				OnPause();
			}
		}

		protected virtual void OnPause()
		{
		}

		private void OnDestroy()
		{
			MonoManager.current.RemoveMethod(UpdateAction);
		}
	}
}
